<?php
namespace jy_area_dian;
defined('BY_JYA') or exit('error');
/*
	分销插件会员类
	如果将分销商各项数据在每次数据变动时计算好并存入数据库。
	那么分别需要做
		在设置上下级关系时，修改新上级的 下线数量、分销商数量、订单数量、订单佣金数量等数据
		在变更上下级关系时，修改原、新上级的各项参数
		在发生订单时，修改上级的订单相关参数
		在用户成为分销商时，修改上级的分销商数量相关数据
*/
class member extends \plugin{
	
	/*
		会员注册
	*/
	public function checklogin(){
		global $_AR;
		if(!$_SESSION['uid']){
			$_SESSION['forward_url']=$_AR['access_json'];
			$res=model('core/core')->checklogin('','',true);
			return $res;
		}else{
			$member=pdo_get('core_members',array('id'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']),array('id','fromuser','fromcode'));
			if(empty($member)){
				model('core/member')->logout();
				$res=model('core/core')->checklogin('','',true);
				return $res;
			}else{
				$user=pdo_get('jy_area_dian_member',array('mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']),array('id'));
				if(!$user['id']){
					if($_SESSION['dian_id']){
						$dian_id=$_SESSION['dian_id'];
					}else{
						$fromuser=$member['fromuser']?:$_SESSION['fromuser'];
						$dian_id=pdo_getcolumn('jy_area_dian_agent',array('mid'=>$fromuser,'uniacid'=>$_SESSION['uniacid']),'id');
					}
					$insert=array(
						'mid'=>$_SESSION['uid'],
						'uniacid'=>$_SESSION['uniacid'],
						'dian_id'=>$dian_id,
						'createtime'=>TIMESTAMP,
					);
					pdo_insert('jy_area_dian_member',$insert);
					$this->update_level($dian_id);
				}
			}
		}
		return '';
	}
	/*
		申请成为自提点
	*/
	public function apply_agent($post){
		if(!$post['area_code']){
			//exi('获取区域code失败！','error');
		}
		$check=$this->getcolumn('agent',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$post['mid'],'pcd'=>$post['pcd']),'status');
		if($check==1){
			exi('您已经代理了该地区，请勿重复代理！','error');
		}
		if($this->setting['agent_type']==1){
			//独家代理模式下，不允许重复
			$check=$this->getcolumn('agent',array('uniacid'=>$_SESSION['uniacid'],'pcd'=>$post['pcd']),'mid');
			if($check && $check!=$post['mid']){
				exi('该区域已存在代理！请更换！','error');
			}
		}
		if($this->setting['agent_many']==0){
			//不支持代理多个地区的情况下，此处检查
			$agent=$this->getcolumn('agent',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$post['mid']),'status');
			if($agent==1){
				exi('您已经是自提点了，不能重复申请！','error');
			}
		}
		$new_apply=array(
			'name'=>$post['name'],
			'mobile'=>$post['mobile'],
			'title'=>$post['title'],
			'status'=>0,
			'area_type'=>pdo_getcolumn('core_area',array('code'=>$post['area_code']),'type'),
			'area_code'=>$post['area_code'],
			'pcd'=>$post['pcd'],
			'address'=>$post['address'],
			'lat_lng'=>$post['lat_lng'],
			'codeimg'=>$post['codeimg'],
		);
		if($post['id']){
			$this->update('agent',$new_apply,array('uniacid'=>$_SESSION['uniacid'],'id'=>$post['id']));
		}else{
			$new_apply['uniacid']=$_SESSION['uniacid'];
			$new_apply['mid']=$post['mid'];
			$new_apply['createtime']=TIMESTAMP;
			$this->insert('agent',$new_apply);
			$id=$this->insertid();
		}
		return $id;
	}
	/*
		审核自提点
	*/
	public function check_agent($agent_id,$status,$remark=''){
		if(!$agent_id || !$status){
			return;
		}
		$agent=$this->get('agent',array('id'=>$agent_id));
		if(!$agent){
			return;
		}
		$update=array(
			'status'=>$status,
			'check_time'=>TIMESTAMP,
		);
		if($update['status']==1){
			//审核通过，如果会员还不是自提点，此处修改
		}else{
			$update['remark']=$remark;
		}
		$this->update('agent',$update,array('id'=>$agent_id));
		return true;
	}
	/*
		禁用自提点
	*/
	public function stop_agent($agent_id){
		$this->update('agent',array('stop'=>1),array('mid'=>$agent_id));
	}
	/*
		恢复自提点
	*/
	public function start_agent($agent_id){
		$this->update('agent',array('stop'=>0),array('mid'=>$agent_id));
	}
	public function mydian($dian_id=0){
		if(!$dian_id){
			$dian_id=pdo_getcolumn('jy_area_dian_member',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']),'dian_id');
		}
		if($dian_id){
			$mydian=pdo_get('jy_area_dian_agent',array('uniacid'=>$_SESSION['uniacid'],'id'=>$dian_id));
		}
		return $mydian;
	}
	/*
		升级分销商等级
		先检测会员身份是否为正常分销商
		然后获取分销商当前等级的下一等级升级条件
		然后逐个条件对比，看是否满足升级条件
		如果全部满足，则升级到相应等级		
		何时触发升级分销商等级检测呢？		
	*/
	public function update_level($dian_id,$orderid=0){
		//
		$member=$this->get('agent',array('id'=>$dian_id,'uniacid'=>$_SESSION['uniacid']));
		if(!$member['status'] || $member['stop']){
			return;
		}
		$level=$this->get('level',array('id'=>$member['level_id']));
		$parameter=array(
			'params'=>array('uniacid'=>$_SESSION['uniacid'],'level >'=>$level['level']),
			'orderby'=>'level desc',
		);
		$levels=$this->getall('level',$parameter['params'],'','',$parameter['orderby']);
		if(empty($levels)){
			return;
		}
		$level_up_types=$this->model()->current_level_up_types();
		foreach($levels as $lv){
			$conditions=json_decode($lv['conditions'],true);
			if(!$level_up_types || !$lv['id']){
				break;
			}
			$up=$is_level_up_one?false:true;
			foreach($level_up_types as $type){
				//留空默认满足
				if($type=='order_price'){
					//团订单总金额（元）,团订单总数（个）
					$member['order_price']=pdo_getsum('jy_area_dian_order',array('ispay'=>1,'dian_id'=>$member['id']),'price');
					if($member['order_price']<$conditions['order_price']){
						$up=false;
						break;
					}
				}elseif($type=='order_num'){
					//团订单总金额（元）,团订单总数（个）
					$member['order_num']=pdo_count('jy_area_dian_order',array('ispay'=>1,'dian_id'=>$member['id']));
					if($member['order_num']<$conditions['order_num']){
						$up=false;
						break;
					}
				}elseif($type=='member_num'){
					$member['member_num']=pdo_count('jy_area_dian_member',array('dian_id'=>$member['id']));
					if($member['member_num']<$conditions['member_num']){
						$up=false;
						break;
					}
				}
			}
			if($up){
				$next_level=$lv;
				break;
			}
		}
		if($up){
			$update=array(
				'level_id'=>$next_level['id'],
				'updatetime'=>time(),
			);
			$this->update('agent',$update,array('mid'=>$mid));
		}
		return true;
	}
}